#include"book.h"
//节点的创建
Node* BuySListNode(TYPE x)
{
        Node* newnode = (Node*)malloc(sizeof(Node));
        if (!newnode){
               perror("newnode!");
               return NULL;
        }
        newnode->date = x;
        newnode->next = NULL;
        return newnode;
}
//尾插(修改实参，二级指针)
void SListPushBack(Node** pplist, TYPE x)
{
        assert(pplist);
        Node* newnode = BuySListNode(x);
        //空、非空
        if (!*pplist)
               *pplist = newnode;
        else {
               Node* cur = *pplist;
               while (cur->next)
                       cur = cur->next;
               cur->next = newnode;
        }
        return;
}
//尾删
void SListPopBack(Node** pplist)
{
        assert(pplist);
        //空
        if (!*pplist)
               return;
        //只有一个节点
        else if (!((*pplist)->next))
        {
               free(*pplist);
               *pplist = NULL;
        }
        //多个节点
        else {
               Node* prev = NULL;
               Node* cur = *pplist;
               //先找到最后一个节点,保存上一节点
               while (cur->next) {
                       prev = cur;
                       cur = cur->next;
               }
               free(cur);
               prev->next = NULL;
        }
        return;
}
//头插
void SListPushHead(Node** pplist, TYPE x)
{
        assert(pplist);
        Node* newnode = BuySListNode(x);
        //空
        if (!*pplist)
               *pplist = newnode;
        //非空
        else {
               newnode->next = *pplist;
               *pplist = newnode;
        }
        return;
}
//头删
void SListPopHead(Node** pplist)
{
        assert(pplist);
        //空
        if (!*pplist)
               return;
        //else if (!(*pplist)->next)
        //{
        //      free(*pplist);
        //      *pplist = NULL;
        //}
        //else {
        //      //头删时需要一个新的指针来指向原空间；
        //      Node* delNode = *pplist;
        //      *pplist = (*pplist)->next;
        //      free(delNode);
        //      delNode = NULL;
        //}
        else {
               //头删时需要一个新的指针来指向原空间；
               Node* delNode = *pplist;
               *pplist = delNode->next;
               free(delNode);
               delNode = NULL;
        }
        //可将一个节点与多个节点的情况合并
        return;
}
//pos后插入（一级指针）
void SListInsertAfter(Node* pos, TYPE x)
{
        assert(pos);//传的是节点的地址
        Node* newnode = BuySListNode(x);
        newnode->next = pos->next;
        pos->next = newnode;
        return;
}
//pos后删除
void SListEraseAfter(Node* pos, TYPE x)
{
        if (!pos || !pos->next)
               return;//pos为NULL或pos是最后一个时直接退出
        Node* delNode = pos;
        pos->next = delNode->next;
        free(delNode);
        return;
}
//查找
Node* SListFind(Node* pplist, TYPE x)
{
        if (!pplist)
               return NULL;
        Node* cur = pplist;
        while (cur) {
               if (cur->date == x)
                       return cur;
               cur = cur->next;
        }
        return NULL;
}
//打印
void PrintSList(Node* pplist)
{
        Node* cur = pplist;
        while (cur)
        {
               printf("%d-->", cur->date);
               cur = cur->next;
        }
        printf("NULL\n");
        return;
}
//测试
void TestNode()
{
        Node* pplist = NULL;
        SListPushBack(&pplist, 1);
        SListPushBack(&pplist, 2);
        SListPushBack(&pplist, 3);
        SListPushBack(&pplist, 4);
        SListPushBack(&pplist, 5);
        SListPushHead(&pplist, 7);
        PrintSList(pplist);
        /*Node* ret = SListFind(pplist,100);
        if (ret)
        {
               printf("4 is in SList\n");
        }
        else
        {
               printf("4 is not in SList\n");
        }*/
        //SListPopBack(&pplist);
        //PrintSList(pplist);
        //SListPopBack(&pplist);
        //PrintSList(pplist);
        //SListPopBack(&pplist);
        //PrintSList(pplist);
        //SListPopBack(&pplist);
        //PrintSList(pplist);
        //SListPopBack(&pplist);
        //PrintSList(pplist);
}
